package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.SportsProjectFile;
import org.jeecg.modules.nikeProject.entity.vo.AddProjectFileVO;
import org.jeecg.modules.nikeProject.entity.vo.OssFileVO;
import org.jeecg.modules.nikeProject.mapper.SportsProjectFileMapper;
import org.jeecg.modules.nikeProject.service.ISportsProjectFileService;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.mapper.OssFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description: 案例上传视频，图片
 * @Author: jeecg-boot
 * @Date:   2024-12-12
 * @Version: V1.0
 */
@Service
public class SportsProjectFileServiceImpl extends ServiceImpl<SportsProjectFileMapper, SportsProjectFile> implements ISportsProjectFileService {

    @Autowired
    private SportsProjectFileMapper sportsProjectFileMapper;
    @Autowired
    private OssFileMapper ossFileMapper;

    @Override
    public void updateFileList(List<SportsProjectFile> sportsProjectFiles, String username) {
        if (null != sportsProjectFiles && sportsProjectFiles.size() > 0) {
            sportsProjectFiles.forEach(x -> {
                x.setIzActive(CommonConstant.COMMON_STATUS_Y);
                x.setUpdateBy(username);
                x.setUpdateTime(new Date());
                sportsProjectFileMapper.updateById(x);
//                OssFile ossFile = ossFileMapper.selectById(x.getOssFileId());
//                ossFileMapper.deleteById(ossFile);
            });
        }
    }

    @Override
    @Transactional
    public Result<?> addProjectFile(AddProjectFileVO addProjectFileVO) {
        SportsProjectFile sportsProjectFile = new SportsProjectFile();
        sportsProjectFile.setProjectId(addProjectFileVO.getProjectId());
        sportsProjectFile.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsProjectFile.setFileType(CommonConstant.OSS_FILE_TYPE_VIDEO);
        sportsProjectFile.setOssFileId(addProjectFileVO.getOssFileId());
        sportsProjectFile.setCreateBy(addProjectFileVO.getUsername());
        sportsProjectFile.setCreateTime(new Date());
        sportsProjectFileMapper.insert(sportsProjectFile);


        OssFile ossFile = ossFileMapper.selectById(addProjectFileVO.getOssFileId());
        if (null == ossFile) {
            ossFile = new OssFile();
            ossFile.setId(addProjectFileVO.getOssFileId());
            ossFile.setFileName(addProjectFileVO.getFileName());
            ossFile.setUrl(addProjectFileVO.getUrl());
            ossFileMapper.insert(ossFile);
        }
        return Result.OK("新增上传文件成功！");
    }

    @Override
    public void updateOssId(String fileId, String projectId, String username, String fileType) {
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectFile::getOssFileId, fileId);
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        SportsProjectFile sportsProjectFile = sportsProjectFileMapper.selectOne(lambdaQueryWrapper);
        //不存在则更新
        if (null == sportsProjectFile) {
            this.addOssFile(fileId, username, projectId, fileType, null);
        }
    }

    /**
     * 新增案例对应的文件地址
     * @param fileId
     * @param username
     * @param projectId
     */
    @Override
    public void addOssFile(String fileId, String projectId, String username, String fileType, String sourceType){
        SportsProjectFile sportsProjectFile = new SportsProjectFile();
        sportsProjectFile.setFileType(fileType);
        sportsProjectFile.setCreateBy(username);
        sportsProjectFile.setCreateTime(new Date());
        sportsProjectFile.setId(IdWorker.getIdStr());
        sportsProjectFile.setOssFileId(fileId);
        sportsProjectFile.setProjectId(projectId);
        if (null != sourceType || !"".equals(sourceType)) {
            sportsProjectFile.setSourceType(sourceType);
        }
        sportsProjectFileMapper.insert(sportsProjectFile);
    }

    @Override
    public List<SportsProjectFile> getFileList(String projectId, String fileType) {
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsProjectFile::getFileType, fileType);
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<SportsProjectFile> getProjectFiles(String projectId, List<String> workInfoSchoolIds) {
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.in(SportsProjectFile::getWorkInfoSchoolId, workInfoSchoolIds);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<SportsProjectFile> getProjectFilesByType(String projectId, String fileType) {
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsProjectFile::getFileType, fileType);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    public List<SportsProjectFile> getProjectFilesByTypeAndSourceType(String projectId, String fileType, String sourceType) {
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsProjectFile::getFileType, fileType);
        lambdaQueryWrapper.eq(SportsProjectFile::getSourceType, sourceType);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void batchOssFile(List<OssFileVO> list, String projectId, String username, String fileType) {
        List<SportsProjectFile> projectFiles = new ArrayList<>();
        List<OssFile> ossFiles = new ArrayList<>();
        for (OssFileVO ossFileVO : list) {
            String ossId = new String();
            if (null != ossFileVO) {
                if(null != ossFileVO.getUrl() || !"".equals(ossFileVO.getUrl())) {
                    OssFile ossFile = new OssFile();
                    ossId = IdWorker.getIdStr();
                    ossFile.setId(ossId);
                    ossFile.setFileName(ossFileVO.getFileName());
                    ossFile.setUrl(ossFileVO.getUrl());
                    ossFile.setCreateTime(new Date());
                    ossFile.setCreateBy(username);
                    ossFiles.add(ossFile);
                }
                SportsProjectFile sportsProjectFile = new SportsProjectFile();
                sportsProjectFile.setProjectId(projectId);
                sportsProjectFile.setOssFileId(ossId);
                sportsProjectFile.setFileType(fileType);
                sportsProjectFile.setSourceType(ossFileVO.getSourceType());
                sportsProjectFile.setIzCheck(ossFileVO.getIzCheck());
                sportsProjectFile.setIzActive(CommonConstant.COMMON_STATUS_N);
                projectFiles.add(sportsProjectFile);
            }
        }
        ossFileMapper.batchInsert(ossFiles);
        sportsProjectFileMapper.batchInsert(projectFiles);
    }
}
